var baseUrl = "http://tingapi.ting.baidu.com/v1/restserver/ting"
var apiMethod = {
  search: 'baidu.ting.search.catalogSug',
  detail: 'baidu.ting.song.play',
  lrc: 'baidu.ting.song.lrc',
  recommand: 'baidu.ting.song.getRecommandSongList',
  download: 'baidu.ting.song.downWeb',
  list: 'baidu.ting.billboard.billList'
}
var query = option => ({
  formate: 'json'
})
var request = data =>  new Promise((resolve,reject) => {
  wx.request({
    url: baseUrl,
    data: data,
    header: {},
    method: 'GET',
    dataType: 'json',
    responseType: 'text',
    success: function (res) { 
      resolve(res.data)
    },
    fail: function (res) {
      reject(res)
     },
    complete: function (res) { },
  })
}) 

var search = (keyword) => request({
  method: apiMethod['search'],
  query: keyword
})

var getOnline = (type,paged) => {
  var limit = 12;
  return request({
    method: apiMethod['list'],
    type: type,
    offset: limit * (paged - 1),
    size: limit
  }).then(data => {
    data.billboard.pic_s210 = data.billboard.pic_s210
    data.billboard.pic_s260 = data.billboard.pic_s260
    data.billboard.pic_s444 = data.billboard.pic_s444
    data.billboard.pic_s640 = data.billboard.pic_s640
    data.song_list.forEach(item => {
      item.pic_big = item.pic_big
      item.pic_small = item.pic_small
    })
    return data;
  })
}

var getDetail = id => request({
  method: apiMethod['detail'],
  songid: id
})
module.exports = {
  getDetail,
  getOnline,
  search,
  request,
  apiMethod
}